package com.luoshizhen.lease.web.admin.schedule;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.luoshizhen.lease.model.entity.LeaseAgreement;
import com.luoshizhen.lease.model.enums.LeaseStatus;
import com.luoshizhen.lease.web.admin.service.LeaseAgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ScheduledTasks {

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    // 每天午夜执行检查租赁状态的任务
    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        // 创建一个Lambda更新包装器，用于构建更新条件
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        // 获取当前日期时间
        Date now = new Date();

        // 设置更新条件：租赁结束日期小于或等于当前日期
        updateWrapper.le(LeaseAgreement::getLeaseEndDate, now);
        // 设置更新条件：租赁协议状态等于已签订
        updateWrapper.eq(LeaseAgreement::getStatus, LeaseStatus.SIGNED);
        // 设置更新条件：租赁协议状态在已签订或正在撤回中
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);

        // 执行更新操作，将满足条件的租赁协议状态更新
        leaseAgreementService.update(updateWrapper);
    }
}
